1 00:00:00,480 --> 00:00:04,590 Today I'm going to make an NPC that follows around other characters. 2 00:00:04,590 --> 00:00:10,590 So if you get within a distance of interest, which is 30 studs, he's going to start following you 3 00:00:10,590 --> 00:00:12,830 or any character that's within that distance. 4 00:00:12,840 --> 00:00:14,850 He'll go after the closest one. 5 00:00:14,850 --> 00:00:16,800 So if I get close. 6 00:00:16,830 --> 00:00:18,810 Oh man, now I got a friend. 7 00:00:18,810 --> 00:00:20,520 I can't get rid of him either. 8 00:00:21,490 --> 00:00:23,080 Death will get rid of them. 9 00:00:23,330 --> 00:00:23,650 Right. 10 00:00:23,680 --> 00:00:28,510 But I'm going to go see if I can get Ralph in between me and him. 11 00:00:29,450 --> 00:00:31,400 And then he'll be more interested in Ralph. 12 00:00:34,080 --> 00:00:35,110 There we go. 13 00:00:35,130 --> 00:00:36,450 That's pretty cool. 14 00:00:36,480 --> 00:00:39,550 I and Ralph was moving slow, so he has to keep stopping. 15 00:00:39,570 --> 00:00:40,990 I thought that would be pretty good. 16 00:00:41,010 --> 00:00:42,690 Let's go ahead and get started. 17 00:00:43,540 --> 00:00:43,780 All right. 18 00:00:43,780 --> 00:00:46,330 So I got my MPC work world up. 19 00:00:46,330 --> 00:00:50,140 I haven't I haven't imported any of these into my game yet. 20 00:00:50,380 --> 00:00:51,790 I figured I'd do a few of them. 21 00:00:51,790 --> 00:00:55,450 So we have Ralph right here, and he does this simple patrol. 22 00:00:55,450 --> 00:00:57,010 I'm going to use Ralph. 23 00:00:57,010 --> 00:01:04,060 And if you didn't get him working from the last video, I'll put a link in a file, the URLs file for 24 00:01:04,060 --> 00:01:08,710 Ralph where you can get them in your toolbox, and then just just get them from there. 25 00:01:08,710 --> 00:01:10,060 So we'll go ahead and do that. 26 00:01:10,060 --> 00:01:15,400 Like if you go to the link in the description, you'll go to roadblocks and then you hit the green jet 27 00:01:15,400 --> 00:01:19,270 button and then you go ahead and you'll have Ralph in your inventory. 28 00:01:19,270 --> 00:01:19,630 Yep. 29 00:01:19,630 --> 00:01:20,800 And there's two scripts. 30 00:01:20,800 --> 00:01:22,510 We made them in one of the videos. 31 00:01:22,660 --> 00:01:25,270 All right, Now, this is not going to be Ralph. 32 00:01:25,270 --> 00:01:26,230 I'm going to rename them. 33 00:01:26,230 --> 00:01:28,000 I'm going to name them. 34 00:01:28,000 --> 00:01:29,500 I don't know Cletus. 35 00:01:30,670 --> 00:01:32,440 I don't even know if that's I spell Cletus. 36 00:01:32,440 --> 00:01:32,920 We know that. 37 00:01:32,920 --> 00:01:34,450 That's that's what it is, though. 38 00:01:34,450 --> 00:01:38,380 I'm going to get rid of my thumbnail camera because I don't have it set and then I'm going to get rid 39 00:01:38,380 --> 00:01:41,740 of the waypoints because we're not going go around them. 40 00:01:41,740 --> 00:01:43,690 And then I change the body colors. 41 00:01:43,690 --> 00:01:49,720 I think I used what, yellow and the demo I did, they're just so that they're different. 42 00:01:49,720 --> 00:01:50,920 We don't have the names up. 43 00:01:50,920 --> 00:01:54,040 So this is Cletus and he's looking for a friend. 44 00:01:54,280 --> 00:01:59,320 We're going to go to that move NPC where we did the patrol. 45 00:01:59,320 --> 00:02:02,350 It'll air now, right, Because we don't have waypoints. 46 00:02:02,350 --> 00:02:03,460 That's all right. 47 00:02:03,460 --> 00:02:06,370 I'm going to get rid of just about everything in here. 48 00:02:07,150 --> 00:02:09,820 We just wanted that animate script set. 49 00:02:09,820 --> 00:02:14,590 We want to have our humanoid root part unanchored, things like that so you can move around. 50 00:02:15,030 --> 00:02:15,340 All right. 51 00:02:15,340 --> 00:02:19,090 So I'm going to have a character, the character that he's chasing. 52 00:02:19,090 --> 00:02:20,770 So I've got to be more specific. 53 00:02:20,770 --> 00:02:24,310 I'm going to call this I NPC. 54 00:02:24,310 --> 00:02:25,630 There's going to be a little bit of eye, right? 55 00:02:25,630 --> 00:02:29,500 He's going to he's going to make the decision to find somebody and then follow them. 56 00:02:29,620 --> 00:02:32,770 So I'm also going to get the humanoid root part. 57 00:02:32,770 --> 00:02:43,030 I'm going to call that I hrp and we'll say I NPC dot, humanoid root part and then I'm going to get 58 00:02:43,030 --> 00:02:47,680 the eye him from the I NPC. 59 00:02:47,860 --> 00:02:49,000 There we go. 60 00:02:50,050 --> 00:02:54,730 I humanoid and I'm not doing the weight for child because it's on server side. 61 00:02:54,730 --> 00:02:56,620 We're not doing spawning and stuff like that. 62 00:02:56,620 --> 00:03:01,950 But just be aware that if you start getting errors that the humanoid root part or the humanoid is not 63 00:03:01,990 --> 00:03:06,490 not available, you'll have to start doing those checks right with wait for child or find first child. 64 00:03:08,280 --> 00:03:12,200 Let's get a max distance where he pays attention to people. 65 00:03:12,210 --> 00:03:18,930 So I'll do a local max dist equals and I'll get it. 66 00:03:18,930 --> 00:03:20,310 30 studs. 67 00:03:20,340 --> 00:03:26,160 Now I'm going to need a function to find the nearest NPC or character. 68 00:03:26,160 --> 00:03:28,440 I'll call it find nearest character. 69 00:03:28,560 --> 00:03:38,370 Let's go local function, find nearest char and this is going to be playing characters and non playing 70 00:03:38,370 --> 00:03:39,120 characters. 71 00:03:39,120 --> 00:03:46,710 So I'm going to pass in my i NPC in case I want to move this to a module script. 72 00:03:46,710 --> 00:03:48,450 I want it to be self contained. 73 00:03:48,450 --> 00:03:54,240 So all the arguments in that I'm going to use within the function I'm going to pass in, I'm also going 74 00:03:54,240 --> 00:03:57,480 to pass in the distance of interest. 75 00:03:58,380 --> 00:04:00,240 I'll call it dist of int. 76 00:04:00,780 --> 00:04:05,310 Now I'm going to need a variable called closest. 77 00:04:05,910 --> 00:04:12,150 Let's call it close this HRP closest humanoid root part will key off of that because that holds the 78 00:04:12,150 --> 00:04:16,740 information for position and orientation of characters. 79 00:04:17,460 --> 00:04:24,710 Now I get a I and C variable, right? 80 00:04:24,720 --> 00:04:36,370 And it will get our humanoid root part and we'll get that from the I NPC dot humanoid root part. 81 00:04:36,390 --> 00:04:43,890 We have it up here, but like I said, I might want to move this into a module script so that all NPCs 82 00:04:43,890 --> 00:04:46,320 could find nearest character or all monsters. 83 00:04:46,320 --> 00:04:50,430 May we may want them to find the character using this function. 84 00:04:50,430 --> 00:04:53,880 No need to write it 50 times for 50 different characters. 85 00:04:53,880 --> 00:04:55,740 Just move it into a module script. 86 00:04:55,740 --> 00:05:02,340 So let's do a for loop and I'm going to say for I and V in pairs. 87 00:05:02,610 --> 00:05:05,130 I'm going to look inside the workspace. 88 00:05:05,130 --> 00:05:07,140 I'm going to get the children in the workspace. 89 00:05:07,140 --> 00:05:10,170 So basically I'm getting all the children in the workspace. 90 00:05:10,170 --> 00:05:14,130 This is not the most efficient way of looking through the workspace. 91 00:05:14,130 --> 00:05:20,080 We'll optimize this later, so I'll show you how to optimize it if we don't have time for that. 92 00:05:20,100 --> 00:05:24,990 So I want to get a humanoid root part. 93 00:05:25,200 --> 00:05:32,820 I want to check to see if any of the things in the workspace has a humanoid root part. 94 00:05:32,820 --> 00:05:35,970 As a first child, that means it's a character. 95 00:05:35,970 --> 00:05:38,250 So I'm going to say Fine. 96 00:05:38,250 --> 00:05:41,550 First child of V, right? 97 00:05:41,550 --> 00:05:43,470 V is the thing. 98 00:05:43,980 --> 00:05:50,100 We're going to check to see if there is a humanoid root part associated with the thing. 99 00:05:50,100 --> 00:05:52,980 If there is, it's something worth following. 100 00:05:52,980 --> 00:06:04,560 So I'm going to say if HR P fan, let's see what the distance is to that HRP from r I NPCs humanoid 101 00:06:04,560 --> 00:06:05,370 root part. 102 00:06:05,370 --> 00:06:21,570 So I'm going to say local temp d distance equals i npc humanoid root part dot position minus the one 103 00:06:21,570 --> 00:06:25,890 we just found, the HPP we just found in V's possession. 104 00:06:26,190 --> 00:06:32,250 And then I'm going to do magnitude that gets the distance between the two points. 105 00:06:33,870 --> 00:06:44,760 Then I check if the distance is less than the distance of interest and temp distance is greater than 106 00:06:44,760 --> 00:06:49,020 one, then why did I do that? 107 00:06:49,020 --> 00:06:50,760 Well, this one makes sense, right? 108 00:06:50,760 --> 00:06:55,830 We want to make sure the temp distance is within our distance of interest, but we also want to make 109 00:06:55,830 --> 00:06:59,400 sure it's not us, right, So we can find our self. 110 00:06:59,400 --> 00:07:02,430 So we have to make sure we're at least one stud away. 111 00:07:02,430 --> 00:07:04,530 And you could make it two if you wanted to. 112 00:07:04,530 --> 00:07:05,550 That would be fine. 113 00:07:05,700 --> 00:07:09,000 But we want to make sure it's at least one start away. 114 00:07:09,300 --> 00:07:14,760 Then what we're going to do if they are if that does meet the criteria, they're within our distance, 115 00:07:14,760 --> 00:07:16,020 but not us. 116 00:07:16,410 --> 00:07:19,260 We are going to make the distance of interest now, temp distance. 117 00:07:19,260 --> 00:07:21,030 So it got a little closer. 118 00:07:21,030 --> 00:07:27,990 So now the next loop around in our for loop, we're only going to look for things that are closer than 119 00:07:27,990 --> 00:07:29,940 the last temp distance. 120 00:07:29,940 --> 00:07:32,400 That's how we find the closest one. 121 00:07:32,820 --> 00:07:38,790 All right, so then I'm going to get my closest hr P, and it's going to be that hr P. 122 00:07:40,220 --> 00:07:43,340 What I what I'm going to do when I get down to the bottom here. 123 00:07:43,340 --> 00:07:49,220 After that for loop, I'm going to return to closest air up. 124 00:07:49,250 --> 00:07:52,690 Now, if we didn't find any, that's fine. 125 00:07:52,700 --> 00:07:54,680 This is going to return nil. 126 00:07:55,940 --> 00:08:03,230 Let's scroll down a little bit and make a while loop and I'll do while I'm going to put my weight statement 127 00:08:03,230 --> 00:08:04,430 right in the while loop. 128 00:08:04,430 --> 00:08:09,830 So the weight statement will act as true for my boolean because it is returning a value. 129 00:08:09,830 --> 00:08:12,230 So it considers that as something. 130 00:08:12,230 --> 00:08:13,730 So that's true. 131 00:08:14,090 --> 00:08:18,800 But we're going to wait in between each loop iteration for one second. 132 00:08:18,800 --> 00:08:23,390 So it's kind of handy because that way you don't forget to put your weight in and then crash your machine. 133 00:08:23,870 --> 00:08:32,000 I'm going to do a local closest hr P equal to find nearest char. 134 00:08:32,000 --> 00:08:32,510 You know what? 135 00:08:32,510 --> 00:08:34,640 I should have called that fine nearest HPE. 136 00:08:34,670 --> 00:08:38,000 That would have made more sense, but I'm going to stick with it now. 137 00:08:38,000 --> 00:08:47,750 So I'm going to pass in my i NPC that's defined above and my max distance, which will become the distance 138 00:08:47,750 --> 00:08:50,270 of interest when it gets inside the function. 139 00:08:50,660 --> 00:08:59,540 And then if I get a closest hr p back where within distance of something we're going to get a target 140 00:08:59,540 --> 00:09:01,640 position that we want to go to. 141 00:09:01,760 --> 00:09:10,070 So I'll say target position is the closest HR position and then I want the direction to so let's say 142 00:09:10,070 --> 00:09:19,370 local I d r, So that's my eyes direction where he wants to go and we get my target position minus the 143 00:09:19,370 --> 00:09:22,160 I humanoid route part position. 144 00:09:23,510 --> 00:09:25,250 Dot unit. 145 00:09:25,250 --> 00:09:33,370 And the reason I do this unit is because I want this direction vector to be of length one so I can calculate 146 00:09:33,380 --> 00:09:35,870 how many studs back I want them to stop. 147 00:09:35,870 --> 00:09:41,120 I don't want them to run right into them, but if I don't know the length of this vector, I can't subtract 148 00:09:41,120 --> 00:09:42,680 from my stop position. 149 00:09:42,680 --> 00:09:45,050 That's why I'm doing the unit vector. 150 00:09:45,530 --> 00:09:53,540 Now let's get our AI humanoid and do a move to to get our little AI guy running after the humanoid that 151 00:09:53,540 --> 00:09:55,910 he found the HRP that he found. 152 00:09:55,910 --> 00:09:58,790 So we have two arguments in this move to. 153 00:09:58,790 --> 00:10:06,440 And one of them, if the target's not moving, we can get our target position and move to it. 154 00:10:06,440 --> 00:10:08,840 But remember, we don't want to run right into them. 155 00:10:08,840 --> 00:10:15,350 So let's subtract off I direction times five. 156 00:10:15,350 --> 00:10:18,920 So remember I direction is one stud away. 157 00:10:18,920 --> 00:10:23,450 If we multiply it by five or five studs away, that's cool and all. 158 00:10:23,450 --> 00:10:28,100 But what happens if the guy is moving around and they probably will be? 159 00:10:28,100 --> 00:10:32,270 Well, the second argument is a walk to part. 160 00:10:32,270 --> 00:10:35,030 So this is walk to point. 161 00:10:35,030 --> 00:10:40,370 And if we add a walk to part and the parts moving, it's going to update my walk to point. 162 00:10:40,370 --> 00:10:41,660 It's pretty convenient. 163 00:10:41,660 --> 00:10:46,940 So the closest humanoid route part will be the point that we're going to chase around. 164 00:10:47,480 --> 00:10:48,260 All right. 165 00:10:48,260 --> 00:10:50,330 Let's go ahead and try this out. 166 00:10:50,330 --> 00:10:58,940 I'm going to go to my MPC work and luckily, I got a spawn location right in front of Cletus so I don't 167 00:10:58,940 --> 00:11:00,380 have to run and find them. 168 00:11:01,180 --> 00:11:02,590 Hey, we'll start our game. 169 00:11:05,330 --> 00:11:06,680 There he is. 170 00:11:09,760 --> 00:11:10,660 Here he goes. 171 00:11:11,230 --> 00:11:14,050 And we're running around and he's chasing us. 172 00:11:14,140 --> 00:11:15,730 That's pretty cool. 173 00:11:16,630 --> 00:11:22,330 Oh, and I found out that the move to times out on 8 seconds, I think I said 6 seconds. 174 00:11:22,330 --> 00:11:25,060 And in the other video, I just read that. 175 00:11:25,360 --> 00:11:26,470 So there you go. 176 00:11:26,470 --> 00:11:29,740 You got to you got somebody that's going to be able to chase you around.